/**
* resource init
**/

import axios from 'axios'
import { Notification } from 'element-ui'
import Store from './stores/index'

const resource = axios.create();

resource.interceptors.request.use(function (config) {
    // Do something before request is sent
    config.method = config.method || "get"
    if (config.method == "get") {
      let url = [];
      if(config.data) {
        Object.keys(config.data).forEach((key) => {
          url.push(`${key}=${config.data[key]}`)
        })
        config.url += `?${url.join('&')}`
      }
      delete config.data
    }
    return config;
  }, function (error) {
    // Do something with request error
    Notification({
      message: error,
      type: "error"
    })
    return Promise.reject(error);
  });

// Add a response interceptor
resource.interceptors.response.use(function (response) {
    // Do something with response data
    if (response.data.status == '0000') {
      return response.data;
    } else {
      Notification({
        message: response.data.message || '接口访问失败',
        type: "error"
      })
      Store.commit('setLoading')
      return Promise.reject(response);
    }
  }, function (error) {
    // Do something with response error
    Notification({
      message: error,
      type: "error"
    })
    Store.commit('setLoading')
    return Promise.reject(error);
  });


export default  resource
